/*
 * Copyright (C) 2004, Velocityme Partnership
 * TaskActions.java
 *
 * Created on 12 September 2004, 01:56
 */

package com.velocityme.www.interfaces.nodeactions;

import com.velocityme.entity.PermissionBean;
import com.velocityme.interfaces.*;
import com.velocityme.utility.DurationRemaining;
import com.velocityme.utility.InvalidKeyException;
import com.velocityme.utility.InvalidTimeException;
import com.velocityme.valueobjects.*;
import com.velocityme.www.actionforms.ChangeStateActionForm;
import com.velocityme.www.actionforms.PersonTimeActionForm;
import com.velocityme.www.actionforms.QuickTimeActionForm;
import com.velocityme.www.actionforms.ReassignResponsibilityActionForm;
import com.velocityme.www.actionforms.TaskDependencyIdActionForm;
import com.velocityme.www.actionforms.TimeActionForm;
import com.velocityme.www.actionforms.TimeIdActionForm;
import com.velocityme.www.dto.BurnDownDTO;
import com.velocityme.www.dto.GanttChartDTO;
import com.velocityme.www.dto.IntegerString;
import com.velocityme.www.dto.TaskDependencyDTO;
import com.velocityme.www.dto.TimeDTO;
import com.velocityme.www.interfaces.ShowNodeAction;
import com.velocityme.www.utility.NodeUtil;
import java.io.*;
import java.rmi.RemoteException;
import java.util.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author  Robert
 */
public class TaskActions extends NodeActions {
    
    /** Creates a new instance of TaskActions */
    public TaskActions() {
    }
    
    public ActionForward changeState(ActionMapping mapping, 
                                     ActionForm form,
                                     HttpServletRequest request,
                                     HttpServletResponse response) throws Exception {
        try {
            HttpSession session = request.getSession();
            KeySession key = (KeySession)session.getAttribute("sessionKey");
            RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
            ChangeStateActionForm changeStateActionForm = (ChangeStateActionForm)form;
            NodePK nodePK = new NodePK((Integer)session.getAttribute("nodeId"));
            TaskPK taskPK = new TaskPK((Integer)session.getAttribute("nodeId"));
            if (request.getParameter("btnSubmit").equals("OK")) {
                TransitionPK transitionPK = new TransitionPK(changeStateActionForm.getTransitionId());
                Byte percentageComplete = new Byte(changeStateActionForm.getPercentageComplete());
                String message = changeStateActionForm.getMessage();
                TimeValue timeValue = new TimeValue();
                timeValue.setDescription(message);
                StateValue stateValue = new StateValue();
                stateValue.setPrimaryKey(new StatePK(new Integer(changeStateActionForm.getStateId())));
                timeValue.setStateValue(stateValue);
                File file = new File(changeStateActionForm.getFile().getFileName());
                String contentType = changeStateActionForm.getFile().getContentType();
                InputStream stream = changeStateActionForm.getFile().getInputStream();
                Calendar start = Calendar.getInstance(); start.clear();
                Calendar stop = Calendar.getInstance(); stop.clear();
                start.set(changeStateActionForm.getYear(), changeStateActionForm.getMonth(), changeStateActionForm.getDay(), changeStateActionForm.getStartHour(), changeStateActionForm.getStartMinute(), 0);
                stop.set(changeStateActionForm.getYear(), changeStateActionForm.getMonth(), changeStateActionForm.getDay(), changeStateActionForm.getStopHour(), changeStateActionForm.getStopMinute(), 0);
                timeValue.setStartTime(start.getTime());
                timeValue.setStopTime(stop.getTime());
                timeValue.setRemainingDuration_min(changeStateActionForm.getDurationTotalMins());
                if (changeStateActionForm.getAssignTime())
                    cs.taskTimeAssign(key, taskPK, percentageComplete, timeValue);
                cs.taskChangeState(key, taskPK, transitionPK, message, percentageComplete, file, contentType, stream);
            }
            // Cancel simply displays the original user again
            else {
            }
            request.setAttribute("nodePK", nodePK);
            return mapping.findForward("ACTION_SUCCESS");
        }
        catch(InvalidTimeException e) {
            ActionErrors errors = new ActionErrors();
            errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.start.before.stop"));
            saveErrors(request, errors);
        }
        return mapping.findForward("ACTION_RETRY");
    }
    
    public ActionForward reassignResponsibility(ActionMapping mapping, 
                                                ActionForm form,
                                                HttpServletRequest request,
                                                HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        ReassignResponsibilityActionForm reassignResponsibilityActionForm = (ReassignResponsibilityActionForm)form;
        NodePK nodePK = new NodePK((Integer)session.getAttribute("nodeId"));
        TaskPK taskPK = new TaskPK((Integer)session.getAttribute("nodeId"));
        if (request.getParameter("btnSubmit").equals("OK")) {
            ContactablePK[] contactablesPK = reassignResponsibilityActionForm.getResponsibleContactablePKs();
            String message = reassignResponsibilityActionForm.getMessage();
            File file = new File(reassignResponsibilityActionForm.getFile().getFileName());
            String contentType = reassignResponsibilityActionForm.getFile().getContentType();
            InputStream stream = reassignResponsibilityActionForm.getFile().getInputStream();
            Calendar start = Calendar.getInstance(); start.clear();
            Calendar stop = Calendar.getInstance(); stop.clear();
            cs.taskReassignResponsibility(key, taskPK, contactablesPK, message, file, contentType, stream);
        }
        // Cancel simply displays the original user again
        else {
        }
        request.setAttribute("nodePK", nodePK);
        return mapping.findForward("ACTION_SUCCESS");
    }
    
    public ActionForward toggleInterested(ActionMapping mapping, 
                                          ActionForm form,
                                          HttpServletRequest request,
                                          HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        NodePK nodePK = new NodePK((Integer)session.getAttribute("nodeId"));
        TaskPK taskPK = new TaskPK((Integer)session.getAttribute("nodeId"));
        if (request.getParameter("btnSubmit").equals("OK")) {
            cs.taskToggleInterestedContactableStateValue(key, taskPK);
        }
        // Cancel simply displays the original user again
        else {
        }
        request.setAttribute("nodePK", nodePK);
        return mapping.findForward("ACTION_SUCCESS");
    }
    
    public ActionForward showDefaultTime(ActionMapping mapping, 
                                         ActionForm form,
                                         HttpServletRequest request,
                                         HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        PersonTimeActionForm taskTimeActionForm = new PersonTimeActionForm();
        taskTimeActionForm.setNodeId(nodePK.getNodeId().intValue());
        Calendar now = Calendar.getInstance();
        now.add(Calendar.DATE, -7);
        taskTimeActionForm.setDate(now.getTime());
        return showTime(mapping, taskTimeActionForm, request, response);
    }

    public ActionForward showTime(ActionMapping mapping, 
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        PersonTimeActionForm taskTimeActionForm = (PersonTimeActionForm)form;
        NodePK nodePK = new NodePK(new Integer(taskTimeActionForm.getNodeId()));
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        NodeValue nodeValue = cs.getNodeValue(key, nodePK);
        ShowNodeAction.setupTreeDisplay(session, nodeValue, "Time");
        TaskPK taskPK = new TaskPK(nodePK.getNodeId());
        Calendar start = Calendar.getInstance();
        start.setTime(taskTimeActionForm.getDate());
        String btnSubmit = request.getParameter("btnSubmit");
        if (btnSubmit != null) {
            if (btnSubmit.equals("<")) {
                start.add(Calendar.DATE, -1);
            }
            if (btnSubmit.equals(">")) {
                start.add(Calendar.DATE, 1);
            }
        }
        taskTimeActionForm.setDate(start.getTime());
        Iterator i = cs.getTaskTimeValues(key, taskPK, start.getTime()).iterator();
        Collection timeDTOs = new ArrayList();
        long totalDuration = 0;
        PermissionPK permissionTimeEdit = new PermissionPK(new Integer(PermissionBean.TIME_EDIT));
        PermissionPK permissionTimeDelete = new PermissionPK(new Integer(PermissionBean.TIME_DELETE));
        while(i.hasNext()) {
            TimeValue timeValue = (TimeValue)i.next();
            Set assignedPermissions = cs.getAssignedPermissions(key, timeValue.getTaskValue().getNodeValue().getPrimaryKey());
            boolean isEditable = assignedPermissions.contains(permissionTimeEdit);
            boolean isDeletable = assignedPermissions.contains(permissionTimeDelete);
            int timeId = timeValue.getTimeId().intValue();
            int taskId = timeValue.getTaskValue().getTaskId().intValue();
            String taskName = timeValue.getTaskValue().getNodeValue().getName();
            String personName = timeValue.getPersonValue().getContactableValue().getNodeValue().getName();
            String description = timeValue.getDescription();
            String stateName = timeValue.getStateValue().getNodeValue().getName();
            Date startTime = timeValue.getStartTime();
            Date stopTime = timeValue.getStopTime();
            long duration = stopTime.getTime() - startTime.getTime();
            totalDuration += duration;
            int remainingDuration_min = timeValue.getRemainingDuration_min().intValue();
            timeDTOs.add(new TimeDTO(isEditable, isDeletable, timeId, taskId, taskName, personName, description, stateName, startTime, stopTime, duration, remainingDuration_min));
        }
        session.setAttribute("taskTimeValue", timeDTOs);
        session.setAttribute("taskDuration", new Long(totalDuration));
        session.setAttribute("years", NodeUtil.getInstance().getYears());
        session.setAttribute("months", NodeUtil.getInstance().getMonths());
        session.setAttribute("days", NodeUtil.getInstance().getDays());
        session.setAttribute("taskTimeActionForm", taskTimeActionForm);
        return mapping.findForward("ACTION_DISPLAY");
    }
    
    public ActionForward showCreateTime(ActionMapping mapping, 
                                        ActionForm form,
                                        HttpServletRequest request,
                                        HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        session.setAttribute("timeValue", new TimeValue());
        TaskPK taskPK = new TaskPK(nodePK.getNodeId());
        TaskValue taskValue = cs.getTaskValue(key, taskPK);
        StateValue[] stateValues = cs.getTaskStateValues(key, taskPK);
        Collection integerStrings = new ArrayList();
        for (int i = 0; i < stateValues.length; i++)
            integerStrings.add(new IntegerString(stateValues[i].getStateId(), stateValues[i].toString()));
        session.setAttribute("statePairs", integerStrings);
        Collection completeIntegerStrings = new ArrayList();
        for (int j = 0; j <= 100; j+=5)
            completeIntegerStrings.add(new IntegerString(new Integer(j), Integer.toString(j)));
        session.setAttribute("completePairs", completeIntegerStrings);
        session.setAttribute("years", NodeUtil.getInstance().getYears());
        session.setAttribute("months", NodeUtil.getInstance().getMonths());
        session.setAttribute("days", NodeUtil.getInstance().getDays());
        session.setAttribute("hours", NodeUtil.getInstance().getHours());
        session.setAttribute("durationDays", NodeUtil.getInstance().getDurationDays());
        session.setAttribute("durationHours", NodeUtil.getInstance().getDurationHours());
        session.setAttribute("minutes", NodeUtil.getInstance().getMinutes());
        // setup default values
        Date now = new Date();
        TimeActionForm timeActionForm = new TimeActionForm();
        timeActionForm.setTaskId(taskValue.getTaskId().intValue());
        timeActionForm.setPercentageComplete(taskValue.getPercentageComplete().byteValue());
        timeActionForm.setDurationTotalMins(taskValue.getRemainingDuration_min());
        timeActionForm.setStartDate(now);
        timeActionForm.setStopDate(now);
        timeActionForm.setStateId(taskValue.getStateValue().getStateId().intValue());
        session.setAttribute("timeActionForm", timeActionForm);
        return mapping.findForward("ACTION_TIME_EDIT");                                            
    }
    
    public ActionForward showTimeEdit(ActionMapping mapping, 
                                      ActionForm form,
                                      HttpServletRequest request,
                                      HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        TimeIdActionForm timeIdActionForm = (TimeIdActionForm)form;
        TimeValue timeValue = cs.getTimeValue(key, new TimePK(new Integer(timeIdActionForm.getTimeId())));
        session.setAttribute("timeValue", timeValue);
        TaskValue taskValue = timeValue.getTaskValue();
        TaskPK taskPK = taskValue.getPrimaryKey();
        StateValue[] stateValues = cs.getTaskStateValues(key, taskPK);
        Collection integerStrings = new ArrayList();
        for (int i = 0; i < stateValues.length; i++)
            integerStrings.add(new IntegerString(stateValues[i].getStateId(), stateValues[i].toString()));
        session.setAttribute("statePairs", integerStrings);
        Collection completeIntegerStrings = new ArrayList();
        for (int j = 0; j <= 100; j+=5)
            completeIntegerStrings.add(new IntegerString(new Integer(j), Integer.toString(j)));
        session.setAttribute("completePairs", completeIntegerStrings);
        session.setAttribute("years", NodeUtil.getInstance().getYears());
        session.setAttribute("months", NodeUtil.getInstance().getMonths());
        session.setAttribute("days", NodeUtil.getInstance().getDays());
        session.setAttribute("hours", NodeUtil.getInstance().getHours());
        session.setAttribute("durationDays", NodeUtil.getInstance().getDurationDays());
        session.setAttribute("durationHours", NodeUtil.getInstance().getDurationHours());
        session.setAttribute("minutes", NodeUtil.getInstance().getMinutes());
        // setup default values
        TimeActionForm timeActionForm = new TimeActionForm();
        timeActionForm.setTaskId(taskValue.getTaskId().intValue());
        timeActionForm.setPercentageComplete(taskValue.getPercentageComplete().byteValue());
        timeActionForm.setDurationTotalMins(timeValue.getRemainingDuration_min());
        timeActionForm.setStartDate(timeValue.getStartTime());
        timeActionForm.setStopDate(timeValue.getStopTime());
        timeActionForm.setStateId(timeValue.getStateValue().getStateId().intValue());
        session.setAttribute("timeActionForm", timeActionForm);
        return mapping.findForward("ACTION_TIME_EDIT");                                            
    }
    
    public ActionForward timeDeleteCheck(ActionMapping mapping, 
                                         ActionForm form,
                                         HttpServletRequest request,
                                         HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        TimeIdActionForm timeIdActionForm = (TimeIdActionForm)form;
        TimeValue timeValue = cs.getTimeValue(key, new TimePK(new Integer(timeIdActionForm.getTimeId())));
        session.setAttribute("timeValue", timeValue);
        session.setAttribute("timeValue", form);
        return mapping.findForward("ACTION_TIME_DELETE");
    }
    
    public ActionForward timeDelete(ActionMapping mapping, 
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        if (request.getParameter("btnSubmit").equals("OK")) {
            TimePK timePK = new TimePK(new Integer(((TimeActionForm)form).getTimeId()));
            cs.taskTimeDelete(key, timePK);
        }
        NodePK nodePK = new NodePK((Integer)session.getAttribute("nodeId"));
        NodeValue nodeValue = cs.getNodeValue(key, nodePK);
        request.setAttribute("nodePK", nodePK);
        if (nodeValue.getType().equals("Task"))
            return mapping.findForward("ACTION_TASK_TIME_SUCCESS");
        else
            return mapping.findForward("ACTION_PERSON_TIME_SUCCESS");
    }
    
    public ActionForward timeEdit(ActionMapping mapping, 
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        TimeActionForm timeActionForm = (TimeActionForm)form;
        NodePK nodePK = new NodePK((Integer)session.getAttribute("nodeId"));
        if (request.getParameter("btnSubmit").equals("OK")) {
            TimeValue timeValue = (TimeValue)session.getAttribute("timeValue");
            timeValue.setDescription(timeActionForm.getDescription());
            TaskPK taskPK = new TaskPK(new Integer(timeActionForm.getTaskId()));
            StatePK statePK = new StatePK(new Integer(timeActionForm.getStateId()));
            Byte percentageComplete = new Byte(timeActionForm.getPercentageComplete());
            timeValue.setStateValue(cs.getStateValue(key, statePK));
            timeValue.setStartTime(timeActionForm.getStartDate());
            timeValue.setStopTime(timeActionForm.getStopDate());
            timeValue.setRemainingDuration_min(timeActionForm.getDurationTotalMins());
            try {
                if (timeValue.timeIdHasBeenSet()) {
                    cs.taskTimeEdit(key, percentageComplete, timeValue);
                }
                else {
                    cs.taskTimeAssign(key, taskPK, percentageComplete, timeValue);
                }
            }
            catch(InvalidTimeException e) {
                ActionErrors errors = new ActionErrors();
                errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.start.before.stop"));
                saveErrors(request, errors);
                return mapping.findForward("ACTION_TIME_RETRY");
            }
        }
        request.setAttribute("nodePK", nodePK);
        NodeValue nodeValue = cs.getNodeValue(key, nodePK);
        if (nodeValue.getType().equals("Task"))
            return mapping.findForward("ACTION_TASK_TIME_SUCCESS");
        else
            return mapping.findForward("ACTION_PERSON_TIME_SUCCESS");
    }
    
    public ActionForward quickTime(ActionMapping mapping, 
                                   ActionForm form,
                                   HttpServletRequest request,
                                   HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        QuickTimeActionForm quickTimeActionForm = (QuickTimeActionForm)form;
        NodePK nodePK = new NodePK((Integer)session.getAttribute("nodeId"));
        TaskPK taskPK = new TaskPK(nodePK.getNodeId());
        cs.taskTimeQuickAdd(key, taskPK, quickTimeActionForm.getHours());
        request.setAttribute("nodePK", nodePK);
        return mapping.findForward("ACTION_SUCCESS");
    }
    
    static public Collection getDependencyDTOs(KeySession p_key, 
                                               RemoteClientSession p_cs, 
                                               Set p_assignedPermissions, 
                                               NodePK p_nodePK, 
                                               boolean p_dependency) throws InvalidKeyException, RemoteException {
        Collection dependencyDTOs = new ArrayList();
        Iterator i;
        if (p_dependency)
            i = p_cs.getTaskDependencyValues(p_key, p_nodePK).iterator();
        else
            i = p_cs.getTaskDependantValues(p_key, p_nodePK).iterator();
        PermissionPK permissionTaskEditDependency = new PermissionPK(new Integer(PermissionBean.TASK_EDIT_DEPENDENCY));
        boolean isThisDeletable = p_assignedPermissions.contains(permissionTaskEditDependency);
        while(i.hasNext()) {
            TaskDependencyValue taskDependencyValue = (TaskDependencyValue)i.next();
            TaskValue taskValue;
            if (p_dependency)
                taskValue = taskDependencyValue.getDependencyTaskValue();
            else
                taskValue = taskDependencyValue.getDependantTaskValue();
            boolean isDeletable = false;
            if (isThisDeletable) {
                p_assignedPermissions = p_cs.getAssignedPermissions(p_key, taskValue.getNodeValue().getPrimaryKey());
                isDeletable = p_assignedPermissions.contains(permissionTaskEditDependency);
            }
            int taskDependencyId = taskDependencyValue.getTaskDependencyId().intValue();
            String nodePathName = p_cs.getNodePathName(p_key, taskValue.getNodeValue().getPrimaryKey());
            dependencyDTOs.add(new TaskDependencyDTO(isDeletable, taskDependencyId, nodePathName));
        }
        return dependencyDTOs;
    }
    
    public ActionForward dependencyDeleteCheck(ActionMapping mapping, 
                                               ActionForm form,
                                               HttpServletRequest request,
                                               HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        session.setAttribute("dependencyValue", form);
        return mapping.findForward("ACTION_DEPENDENCY_DELETE");
    }
    
    public ActionForward dependencyDelete(ActionMapping mapping, 
                                          ActionForm form,
                                          HttpServletRequest request,
                                          HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        NodePK nodePK = new NodePK((Integer)session.getAttribute("nodeId"));
        if (request.getParameter("btnSubmit").equals("OK")) {
            TaskDependencyPK taskDependencyPK = new TaskDependencyPK(new Integer(((TaskDependencyIdActionForm)form).getTaskDependencyId()));
            cs.taskDependencyDelete(key, taskDependencyPK);
        }
        request.setAttribute("nodePK", nodePK);
        return mapping.findForward("ACTION_DEPENDENCY_DELETE_SUCCESS");
    }
    
    public ActionForward showGantt(ActionMapping mapping, 
                                   ActionForm form,
                                   HttpServletRequest request,
                                   HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        NodeValue nodeValue = cs.getNodeValue(key, nodePK);
        ShowNodeAction.setupTreeDisplay(session, nodeValue, "Gantt");
        TaskPK taskPK = new TaskPK(nodePK.getNodeId());
        GanttChartDTO ganttChartDTO = new GanttChartDTO(cs.getTaskValueTree(key, taskPK, 3));
        session.setAttribute("ganttChartDTO", ganttChartDTO);
        return mapping.findForward("ACTION_DISPLAY");
    }
    
    static public Collection calculateBurnDownDTOs(Map durations) {
        Collection burnDownDTOs = new ArrayList();
        Iterator i = durations.keySet().iterator();
        while (i.hasNext()) {
            Object label = i.next();
            DurationRemaining dr = (DurationRemaining)durations.get(label);
            int duration_min = dr.getDuration_min();
            int remaining_min = dr.getRemaining_min();
            burnDownDTOs.add(new BurnDownDTO(label.toString(), duration_min, remaining_min));
        }
        return burnDownDTOs;
    }
    
    static public Integer calculateMax(Collection burnDownDTOs) {
        Iterator i = burnDownDTOs.iterator();
        int total = 0;
        while (i.hasNext()) {
            BurnDownDTO bd = (BurnDownDTO)i.next();
            total = Math.max(total, bd.getDuration_min() + bd.getRemaining_min());
        }
        return new Integer(total);
    }
    
    public ActionForward showBurnDown(ActionMapping mapping,
    								  ActionForm form,
    								  HttpServletRequest request,
    								  HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        NodeValue nodeValue = cs.getNodeValue(key, nodePK);
        ShowNodeAction.setupTreeDisplay(session, nodeValue, "BurnDown");
        TaskPK taskPK = new TaskPK(nodePK.getNodeId());
        TaskValue taskValue = cs.getTaskValue(key, taskPK);
        Map durations = cs.getNodeDurationSum(key, nodePK, true, taskValue.getStartDate(), new Date());
        Collection burnDownDTOs = calculateBurnDownDTOs(durations);
        session.setAttribute("burnDownValue", burnDownDTOs);
        session.setAttribute("total", calculateMax(burnDownDTOs));
        return mapping.findForward("ACTION_DISPLAY");
    }
    
}
